package com.qf.boot2302.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 该切面用于统计方法执行的时长
 */
@Aspect
@Component
@Slf4j
public class TimeAspect {

    // 1.定义切点
    @Pointcut("execution(* com.qf.boot2302.controller.UserController.*(..))")
    public void timepointcut(){};

    // 2.定义通知
    @Around("timepointcut()")
    public Object calculateTime(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        // 调用目标方法
        Object proceed = pjp.proceed();
        long end = System.currentTimeMillis();
        // 获取当前要调用的目标方法
        Signature signature = pjp.getSignature();
        log.info("{}请求消耗的时长为:{}ms",signature,(end-start));
        return proceed;
    }



}
